﻿using DTcms.Common;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI
{
    public partial class BaseController : Controller
    {
        /// <summary>
        /// OAuth平台列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public DataTable get_site_oauth_list(int top, string strwhere)
        {
            string _where = "is_lock=0";
            if (!string.IsNullOrEmpty(strwhere))
            {
                _where += " and " + strwhere;
            }
            return new BLL.site_oauth().GetList(top, _where).Tables[0];
        }

        /// <summary>
        /// 返回用户头像图片地址
        /// </summary>
        /// <param name="user_name">用户名</param>
        /// <returns>String</returns>
        public string get_user_avatar(string user_name)
        {
            string avatar = string.Empty;
            BLL.users bll = new BLL.users();
            if (!bll.Exists(user_name))
            {
                return "/images/avatar_45.png";
            }
            avatar = bll.GetCount("avatar", "user_name='" + user_name + "' and status<3");
            if (avatar == "")
            {
                avatar = "/images/avatar_45.png";
            }
            return avatar;
        }
        
        /// <summary>
        /// 返回用户头像图片地址
        /// </summary>
        /// <param name="user_name">用户ID</param>
        /// <returns>String</returns>
        public string get_user_avatar(int user_id)
        {
            string avatar = "/images/avatar_45.png";
            if (user_id == 0)
            {
                return avatar;
            }
            BLL.users bll = new BLL.users();
            if (!bll.Exists(user_id))
            {
                return avatar;
            }
            avatar = bll.GetCount("avatar", "id=" + user_id + " and status<3");
            if (avatar == "")
            {
                avatar = "/images/avatar_45.png";
            }
            return avatar;
        }

        /// <summary>
        /// 返回用户昵称
        /// </summary>
        /// <param name="user_name">用户名</param>
        /// <returns>String</returns>
        public string get_user_nick_name(string user_name)
        {
            BLL.users bll = new BLL.users();
            if (!bll.Exists(user_name))
            {
                return "匿名人员";
            }
            return bll.GetCount("nick_name", "user_name='" + user_name + "' and status<3");
        }

        /// <summary>
        /// 返回用户name
        /// </summary>
        /// <param name="user_name">用户ID</param>
        /// <returns>String</returns>
        public string get_user_name(int userid)
        {
            BLL.users bll = new BLL.users();
            if (!bll.Exists(userid))
            {
                return "匿名人员";
            }
            return bll.GetCount("user_name", "id=" + userid + " and status<3");
        }

        /// <summary>
        /// 返回用户昵称
        /// </summary>
        /// <param name="user_name">用户ID</param>
        /// <returns>String</returns>
        public string get_user_nick_name(int userid)
        {
            BLL.users bll = new BLL.users();
            if (!bll.Exists(userid))
            {
                return "匿名人员";
            }
            return bll.GetCount("nick_name", "id='" + userid + "' and status<3");
        }

        /// <summary>
        /// 统计短信息数量
        /// </summary>
        /// <param name="strwhere">查询条件</param>
        /// <returns>Int</returns>
        public int get_user_message_count(string strwhere)
        {
            return int.Parse(new BLL.user_message().GetCount("count(*)", strwhere));
        }

        /// <summary>
        /// 短信息列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public List<Model.user_message> get_user_message_list(int top, string strwhere)
        {
            return new BLL.user_message().GetModelList(top, strwhere, "*", "id asc,post_time desc");
        }

        /// <summary>
        /// 短信息分页列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Model.user_message> get_user_message_list(int page_size, int page_index, string strwhere, out int totalcount)
        {
            var resultlist = new BLL.user_message().GetQueryPage(page_index, page_size, strwhere, "is_read asc,post_time desc");
            totalcount = Utils.ObjToInt(resultlist.TotalItems);
            return resultlist.Items;
        }

        /// <summary>
        /// 积分明细分页列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Model.user_point_log> get_user_point_list(int page_size, int page_index, string strwhere, out int totalcount)
        {
            var resultlist = new BLL.user_point_log().GetQueryPage(page_index, page_size, strwhere, "add_time desc,id desc");
            totalcount = Utils.ObjToInt(resultlist.TotalItems);
            return resultlist.Items;
        }
        
        /// <summary>
        /// 余额明细分页列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Model.user_amount_log> get_user_amount_list(int page_size, int page_index, string strwhere, out int totalcount)
        {
            var resultlist = new BLL.user_amount_log().GetQueryPage(page_index, page_size, strwhere, "add_time desc,id desc");
            totalcount = Utils.ObjToInt(resultlist.TotalItems);
            return resultlist.Items;
        }

        /// <summary>
        /// 充值记录分页列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Model.user_recharge> get_user_recharge_list(int page_size, int page_index, string strwhere, out int totalcount)
        {
            var resultlist = new BLL.user_recharge().GetQueryPage(page_index, page_size, strwhere, "add_time desc,id desc");
            totalcount = Utils.ObjToInt(resultlist.TotalItems);
            return resultlist.Items;
        }

        /// <summary>
        /// 充值记录列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns></returns>
        public List<Model.user_recharge> get_user_recharge_list(int top, string strwhere)
        {
            return new BLL.user_recharge().GetModelList(top, strwhere, "", "add_time desc,id desc");
        }

        /// <summary>
        /// 用户邀请码列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns></returns>
        public List<Model.user_code> get_user_invite_list(int top, string strwhere)
        {
            string _where = "type='" + DTEnums.CodeEnum.Register.ToString() + "'";
            if (!string.IsNullOrEmpty(strwhere))
            {
                _where += " and " + strwhere;
            }
            return new BLL.user_code().GetModelList(top, _where, "*", "add_time desc,id desc");
        }

        /// <summary>
        /// 返回邀请码状态
        /// </summary>
        /// <param name="str_code">邀请码</param>
        /// <returns>bool</returns>
        public bool get_invite_status(string str_code)
        {
            Model.user_code model = new BLL.user_code().GetModel("status=0 and datediff(d,eff_time,getdate())<=0 and str_code='" + str_code + "'", "top 1 *", "");
            if (model != null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 收货地址列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public List<Model.user_addr_book> get_user_addr_book_list(int top, string strwhere)
        {
            return new BLL.user_addr_book().GetModelList(top, strwhere, "*", "is_default desc,id asc");
        }

        /// <summary>
        /// 收货地址分页列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Model.user_addr_book> get_user_addr_book_list(int page_size, int page_index, string strwhere, out int totalcount)
        {
            var resultlist = new BLL.user_addr_book().GetQueryPage(page_index, page_size, strwhere, "is_default desc,id asc");
            totalcount = Utils.ObjToInt(resultlist.TotalItems);
            return resultlist.Items;
        }
        
        /// <summary>
        /// 会员组别列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns></returns>
        public List<Model.user_groups> get_user_groups_list(int top, string strwhere)
        {
            return new BLL.user_groups().GetModelList(top, strwhere, "*", "grade asc");
        }

        /// <summary>
        /// 返回组别名称
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>String</returns>
        public string get_groups_title(int id)
        {
            string title = new BLL.user_groups().GetCount("title", "id=" + id);
            if (string.IsNullOrEmpty(title))
            {
                return "未找到";
            }
            return title;
        }
        
        /// <summary>
        /// 用户登录记录列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns></returns>
        public List<Model.user_login_log> get_user_log_list(int top, string strwhere)
        {
            return new BLL.user_login_log().GetModelList(top, strwhere, "", "login_time desc,id desc");
        }

        /// <summary>
        /// 返回用户qq联系方式
        /// </summary>
        /// <param name="user_name">用户ID</param>
        /// <returns>String</returns>
        public string get_user_qq(int user_id)
        {
            if (user_id == 0)
            {
                return "";
            }
            BLL.users bll = new BLL.users();
            if (!bll.Exists(user_id))
            {
                return "";
            }
            string qq = bll.GetCount("qq", "id=" + user_id + " and status<3");
            return qq;
        }
        #region  评论
        /// <summary>
        /// 用户评论分页列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Model.article_comment> get_user_comment(string channel_name, int user_id, bool relation, int page_size, int page_index, string strwhere, out int totalcount)
        {
            return new BLL.article_comment().GetList(channel_name, user_id, relation, page_size, page_index, strwhere, "add_time desc,id asc", out totalcount);
        }
        #endregion
    }
}
